
(function(window){

    window.Album = function(sel){

        var _this = this;
        //内部属性
        var banner_w = $(window).width();
        var listBox = sel.currentObj.find('.album-item');

        //初始化方法(对外接口)
        this.init = function(start,numbers){
            listBox.find('.item-list').css('width',banner_w + 'px');
            listBox.css({
                'width': numbers*banner_w + 'px',
                'transition':'',
                '-webkit-transform':'translateX('+-(start*banner_w)+'px)'
            });
            setTimeout(function(){
                listBox.css('transition','-webkit-transform '+sel.speed/1000+'s');
            },50);
            sel.currentObj.find('#title-grade').text(start + 1);
            sel.currentObj.find('#title-grades').text(numbers);
            sel.currentObj.fadeIn()
        };

        //内部事件(运动)
        function slide(direction){
            //如果开启缩放模式(则初始化)
            listBox.find('img').css({
                'left':'0px',
                'top':'0px',
                'transform':'scale(1)'
            });
            var banner_ = direction == 'left'? banner_w : -banner_w;
            var xIng = window.getComputedStyle(listBox[0],null)['webkitTransform'].split(', ')[4];
            //如果正在运动则禁止运动
            if(Math.abs(xIng)%banner_w !=0){return}
            //如果已经到达第一页和最后一页则禁止运动
            if((direction == 'right' && xIng == 0) || (direction == 'left' && xIng == (listBox.children().length-1)*-banner_w) ){
                return ;
            }

            var gradeObj = sel.currentObj.find('#title-grade');
            var isAdd = direction == 'left'? 1 : -1;
                gradeObj.text(Number(gradeObj.text())+isAdd);
            //开始运动
            listBox.css('-webkit-transform','translateX('+(xIng-banner_)+'px)');
        }

        //事件添加
        function evtSlide(){
            var startTime = '',movex = '',movexIng = '';

            //回调事件
            sel.currentObj.find('.title-back').on('tap',function(){
                sel.callback ? sel.callback() : sel.currentObj.fadeOut();
            });

            listBox[0].addEventListener('touchstart',function(e){
                //防止图片缩放写入数据
                movexIng = '';
                startTime = new Date().getTime();
                movex = e.touches[0].clientX;
                //图片缩放功能
                if(sel.scale){
                    if(e.targetTouches.length>=2){
                        initXY = xy = Math.abs(e.targetTouches[0].clientY - e.targetTouches[1].clientY);
                    }else if(e.targetTouches.length ==1){
                        lateX = e.targetTouches[0].clientX;
                        lateY = e.targetTouches[0].clientY;
                    }
                }
            });

            listBox[0].addEventListener('touchmove',function(e){
                movexIng = e.touches[0].clientX;
            });

            listBox[0].addEventListener('touchend',function(e){
                var difference = new Date().getTime() - startTime;
                if(difference<250&&movexIng&&(movexIng-movex)<-60){
                    //console.log('left')
                    slide('left');
                }
                else if(difference<250&&movexIng&&(movexIng-movex)>60){
                    //console.log('right');
                    slide('right');
                }
            });

            //图片缩放功能
            if(sel.scale){
                //*************************************
                var initXY = 0,xy = 0,scaleNum = 0.5;
                var lateX = 0,lateY = 0;
                //*************************************
                listBox.on('touchmove','img',function(e){
                    //获取当前缩放值
                    var scale = window.getComputedStyle(this,null)['webkitTransform'].split(', ')[3];
                    var _this = $(this);
                    //两个手指缩放处理
                    if(e.targetTouches.length>=2){
                        //获取当前移动中的距离
                        var isMax = Math.abs(e.targetTouches[0].clientY - e.targetTouches[1].clientY);
                        //动画运行||缩放距离不够 则阻止
                        if((scale%scaleNum!=0)||(Math.abs(isMax - initXY)<40)){return}/*aaaaaaaaaa*/
                        var dir = scaleNum;
                        if(isMax < xy) {dir = -dir}
                        _this.css('-webkit-transform','scale('+ (Number(scale)+dir) +')');
                        xy = isMax;
                        //当缩放完成后可拖动查看图片
                    }else if(e.targetTouches.length ==1){
                        //如果正在缩放阻止拖动
                        if(scale<=1||(scale%scaleNum!=0)){return}
                        //阻止图片超过边界
                        var nw = (_this.width()-$(window).width())/2;
                        //当前手指移动的距离(正负)
                        var x = e.targetTouches[0].clientX - lateX;
                        var y = e.targetTouches[0].clientY - lateY;
                        //当前图片的偏移距离
                        var x_ = _this.css('left');
                        x_ = x_.substr(0,x_.length-2);
                        var y_ = _this.css('top');
                        y_ = y_.substr(0,y_.length-2);
                        //图片到达边界所做的处理
                        if(Math.round(x_) > (nw-4) && x>0){
                            _this.css({
                                'left':nw + 'px',
                                'top':Number(y_) + y
                            });
                        }else if(Math.round(x_) < -(nw-4)&& x<=0){
                            _this.css({
                                'left':-nw + 'px',
                                'top':Number(y_) + y
                            });
                        }else{
                            _this.css({
                                'left':Number(x_) + x,
                                'top':Number(y_) + y
                            })
                        }
                        lateX = e.targetTouches[0].clientX;
                        lateY = e.targetTouches[0].clientY;
                    }
                })
            }

        }
        //绑定事件
        evtSlide();
    }

})(window);